package com.tangding.shiro.support.redis;

import java.io.Serializable;
import java.util.Collection;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.stereotype.Component;

import com.tangding.common.data.Constants;
import com.tangding.common.logx.LogHelp;
import com.tangding.shiro.support.redis.cache.RedisCache;
import com.tangding.shiro.support.redis.cache.RedisObjectSerializer;

/**
 * @description 
 *
 * @author Ranger
 * @version 2016年8月13日_下午5:42:53
 *
 */
@Component
public class RedisSessionDao extends AbstractSessionDAO {

	private long expire = Constants.LOGIN_SESSION_EXPIRED;
	
	private RedisCache<String, Session> cache = new RedisObjectSerializer<>("_shiro_session_", Session.class);
	
	@Override
	public void delete(Session session) {
		if (session == null || session.getId() == null) {
			LogHelp.doLogAppErr("session or session id is null");
			return;
		}
		
		String sessionId = session.getId().toString();
//		StringRedisCacheService.del(sessionId);
		cache.remove(sessionId);
	}

	@Override
	public Collection<Session> getActiveSessions() {
		/*Set<Session> sessions = new HashSet<Session>();
		Set<String> keys = StringRedisCacheService.keys("*");
		if (keys != null) {
			keys.forEach(key -> {
				Session s = (Session) SerializeUtils.deserialize(Base64.decode(StringRedisCacheService.get(key)));
				sessions.add(s);
			});
		}*/
		
		Collection<Session> sessions = cache.values();
		return sessions;
	}

	@Override
	public void update(Session session) throws UnknownSessionException {
		session.setTimeout(expire == 0 ? -1 : expire * 1000L);// 单位：毫秒
		String key = session.getId().toString();
//		byte[] value = SerializeUtils.serialize(session);
//		StringRedisCacheService.set(key, Base64.encode(value), expire);
		cache.put(key, session);
	}
	
	/**
	 * save session
	 * 
	 * @param session
	 * @throws UnknownSessionException
	 */
	private void saveSession(Session session) throws UnknownSessionException {
		if (session == null || session.getId() == null) {
			LogHelp.doLogAppErr("session or session id is null");
			return;
		}

		session.setTimeout(expire == 0 ? -1 : expire * 1000L);// 单位：毫秒
		
		String key = session.getId().toString();
		cache.put(key, session);
//		byte[] value = SerializeUtils.serialize(session);
//		StringRedisCacheService.set(key, Base64.encode(value), expire);
	}

	@Override
	protected Serializable doCreate(Session session) {
		if (session != null && session.getId() != null) {
			Session s = cache.get(session.getId().toString());
			if (s != null) {
				return s.getId();
			}
		}
		Serializable sessionId = this.generateSessionId(session);
		this.assignSessionId(session, sessionId);
		this.saveSession(session);
		return sessionId;
	}

	@Override
	protected Session doReadSession(Serializable sessionId) {
		if (sessionId == null) {
			LogHelp.doLogAppErr("session id is null");
			return null;
		}

		/*byte[] data = Base64.decode(StringRedisCacheService.get(sessionId.toString()));
		if (data == null) {
			return null;
		}
		
		Session s = (Session) SerializeUtils.deserialize(data);*/
		Session s = cache.get(sessionId.toString());
		return s;
	}
}
